package com.pi.pro.xcmg.am.was.web.cust.controller;


import com.fasterxml.jackson.annotation.JsonView;
import com.pi.common.CodesConstants;
import com.pi.framework.core.exception.ApplicationException;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.StringUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.entity.TwaDocAsnHeader;
import com.pi.wms.entity.TwtActPalletizeList;
import com.pi.wms.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.data.jpa.datatables.mapping.DataTablesOutput;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@Slf4j
@Controller
@RequestMapping("/inbound/otasn")
public class OtAsnController extends BaseAsnController<TwaDocAsnHeader, Long> {


    @Autowired
    private OtAsnService service;

    @Override
	protected ICrudService<TwaDocAsnHeader, Long> getCrudService() {
	    return service;
	}

	@Override
	protected String getPrefix() {
		return "otasn";
	}

	@Override
	protected TwaDocAsnHeader newModel(HttpServletRequest request) {
        TwaDocAsnHeader header=new TwaDocAsnHeader();
        header.setCreateSource(CodesConstants.SYSTEM_WMS);
        header.setCategory(XcmgAmConstants.WORK_SOURCE_OTHER);
        header.setDocPrior(WmsConstants.TASK_PRIOR_NORMAL);
		return header;
	}

	@Override
	protected void prepareDatatableConditions(Map<String, Object> query, DataTablesInput input) {
		Long userId = SecurityUtils.getLoginUserId();
		query.put("userId", userId.toString());
        query.put("category", XcmgAmConstants.WORK_SOURCE_OTHER);
		super.prepareDatatableConditions(query, input);
	}


    @ResponseBody
    @JsonView({DataTablesOutput.View.class})
    @GetMapping({"datatable"})
    @Override
    public DataTablesOutput<TwaDocAsnHeader> datatable(@RequestParam Map<String, Object> q, @Valid DataTablesInput input) {

        DataTablesOutput output=super.datatable(q,input);
        if ((output.getData()==null) && (output.getData().size()==0)){
            return output;
        }

        Map<String, BigDecimal> summ=null;
        Map<String, Object> params=new HashedMap();
        List<TwtActPalletizeList> pals=null;
        String gno="";
        BigDecimal total=null;
        try {
            if ((output.getData()!=null) && (output.getData().size()>0)){
            }
        } catch (Exception e) {
            e.printStackTrace();
            output = new DataTablesOutput();
            output.setError("加载表格数据发生异常");
        }

        return output;
    }

    @GetMapping("del/{id}")
    public String del(@PathVariable("id") Long id, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        model.addAttribute("params", getParams(request));
        String msg="";
        if (id != null) {
            msg=this.delete(id, true, false);
            if (!StringUtils.isNullOrBlank(msg)){
                addErrorMessage(model, "删除失败:"+msg);
                return super.redirect("");
            }
            addSuccessMessage(redirectAttributes, "删除数据成功");
        }
        return super.redirect("");
    }

    @PostMapping("del")
    public String del(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        model.addAttribute("params", getParams(request));
        String msg="";
        if (ids != null && ids.length > 0) {
            for (Long id:ids){
                msg=this.delete(id, true, false);
                if (!StringUtils.isNullOrBlank(msg)){
                    addErrorMessage(model, "删除失败:"+msg);
                    return super.redirect("");
                }
            }
            addSuccessMessage(redirectAttributes, "删除数据成功");
        }
        return super.redirect("");
    }

    @GetMapping("close/{id}")
    public String close(@PathVariable("id") Long id, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        model.addAttribute("params", getParams(request));
        String msg="";
        if (id != null) {
            msg=this.close(id, true, false);
            if (!StringUtils.isNullOrBlank(msg)){
                addWarnMessage(model, "关闭失败:"+msg);
                return super.redirect("");
            }
            addSuccessMessage(redirectAttributes, "关闭数据成功");
        }
        return super.redirect("");
    }

    @PostMapping("close")
    public String close(@RequestParam("ids") Long[] ids, Model model, HttpServletRequest request, RedirectAttributes redirectAttributes) {
        model.addAttribute("params", getParams(request));
        String msg="";
        if (ids != null && ids.length > 0) {
            for (Long id:ids){
                msg=this.close(id, true, false);
                if (!StringUtils.isNullOrBlank(msg)){
                    addWarnMessage(model, "关闭失败:"+msg);
                    return super.redirect("");
                }
            }
            addSuccessMessage(redirectAttributes, "关闭数据成功");
        }
        return super.redirect("");
    }

    protected String redirect(String relativeUrl) {
        if(!StringUtils.isNullOrBlank(relativeUrl) && !relativeUrl.startsWith("/")) {
            relativeUrl = "/" + relativeUrl;
        }
        return "redirect:" + this.referUrl() + relativeUrl;
    }

    protected String root(String relativeUrl) {
        if(!StringUtils.isNullOrBlank(relativeUrl) && !relativeUrl.startsWith("/")) {
            relativeUrl = "/" + relativeUrl;
        }
        return this.referUrl() + relativeUrl;
    }

    private String referUrl(){
        return "/inbound/otasn/";
    }

}
